﻿using dz.u9.jky.schema.Common;
using dz.u9.jky.schema.PM;
using System.Collections.Generic;
using UFIDA.U9.ISV.PM;
using UFIDA.U9.PPR.PurPriceAdjustment;

namespace dz.inner.jky.api.service
{
    internal class PurPriceAdjustmentService : BaseService
    {
        public U9CResult Add(DzPurPriceAdjustmentDTO dto)
        {
            return util.U9Invoke(__Add, dto,dto.Org);
        }

        public void Submit(object parm)
        {
            CommonDoc doc = (CommonDoc)parm;
            SubmitPurPriceAdjustmentSRV sv  = new SubmitPurPriceAdjustmentSRV();

            sv.PurPriceAdjustments = new List<UFIDA.U9.Base.DTOs.IDCodeNameDTO>() { new UFIDA.U9.Base.DTOs.IDCodeNameDTO() { Code = doc.DocNo,ID = doc.ID } };

            sv.Do();
        }

        public void Audit(object doc)
        {
            CommonDoc parm = (CommonDoc)doc;
            ApprovedPurPriceAdjustmentSRV sv = new ApprovedPurPriceAdjustmentSRV();

            sv.PurPriceAdjustments = new List<UFIDA.U9.Base.DTOs.IDCodeNameDTO>() { new UFIDA.U9.Base.DTOs.IDCodeNameDTO() { Code = parm.DocNo, ID = parm.ID } };

            sv.Do();
        }

        private U9CResult __Add(DzPurPriceAdjustmentDTO dto)
        {
            U9CResult result = new U9CResult();

            CreatePurPriceAdjustmentSRV sv = new CreatePurPriceAdjustmentSRV();

            sv.PurPriceAdjustments = Convert(dto);

            var rtn = sv.Do();

            if (rtn[0].ID > 0)
            {
                PurPriceAdjustment.Finder.FindByID(rtn[0].ID);
            }
            result.DocNo = (rtn[0].ID >0)? PurPriceAdjustment.Finder.FindByID(rtn[0].ID).DocNo : string.Empty;
            result.Id = rtn[0].ID;
            result.IsSuccess = true;

            return result;
        }

        private List<PurPriceAdjustmentDTO> Convert(DzPurPriceAdjustmentDTO old)
        {
            PurPriceAdjustmentDTO dto = new PurPriceAdjustmentDTO();

            ObjectCopyUtil util = new ObjectCopyUtil();

            util.CopyProperty(old, ref dto);


            List<PurPriceAdjustmentDTO> rtn = new List<PurPriceAdjustmentDTO>() { dto };

            return rtn;
        }
    }
}
